import time
import re
import cv2
import numpy as np
from Common.Utils import WebDriver

from selenium.webdriver.common.by import By
from PIL import Image
import pytesseract
import io



class Login:
    url = ""
    driver = ""

    def __init__(self):
        self.url = "http://localhost:8080/#/login?redirect=%2Fdashboard"
        self.driver = WebDriver.driver
        self.driver.get(self.url)
        self.driver.implicitly_wait(3)

    def autoIdentifyCode(self):
        self.driver.find_element(By.CSS_SELECTOR, "#app > div > form > div:nth-child(4) > div > div > input").clear()

        # 使用 CSS Selector 定位到验证码图片
        captcha_element = self.driver.find_element(By.CSS_SELECTOR,
                                                   "#app > div > form > div:nth-child(4) > div > span.show-pwd > img")  # 替换为实际的 CSS Selector
        # app > div > form > div:nth-child(4) > div > span.show-pwd > img
        # 获取验证码元素的截图
        screenshot_as_binary = captcha_element.screenshot_as_png

        # 将截图的二进制数据转换为 PIL.Image 对象
        image = Image.open(io.BytesIO(screenshot_as_binary))

        # 将 PIL.Image 对象转换为 OpenCV 图像
        opencv_image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)

        # 图像预处理
        # 灰度化
        gray = cv2.cvtColor(opencv_image, cv2.COLOR_BGR2GRAY)

        # 二值化
        _, binary = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY_INV)

        # 使用中值滤波去噪
        # denoised = cv2.medianBlur(binary, 3)

        # 使用 pytesseract 进行 OCR 识别
        captcha_text = pytesseract.image_to_string(image, config='--psm 8')

        result = captcha_text.replace(" ", "")  # 将所有空格替换为空字符
        result = re.sub(r'[^a-zA-Z0-9]', '', result)
        print(f"识别的验证码是: {result}")

        self.driver.find_element(By.CSS_SELECTOR, "#app > div > form > div:nth-child(4) > div > div > input").send_keys(
            result)

    def Init(self, user, password):
        self.driver.refresh()

        self.driver.find_element(By.CSS_SELECTOR,
                                 "#app > div > form > div:nth-child(2) > div > div.el-input > input").send_keys(
            user)  # 账号
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#app > div > form > div:nth-child(3) > div > div.el-input > input").send_keys(
            password)  # 密码
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#app > div > form > div.el-form-item.user-type.is-required > div > div > label:nth-child(1)"
                                 " > span.el-radio__input > span").click()  # 点击管理员

    # 成功登录：
    def LoginSucTest(self):
        Login.Init(self, "admin", "admin")
        Login.autoIdentifyCode(self)
        time.sleep(5)
        self.driver.find_element(By.CSS_SELECTOR, "#app > div > form > button").click()

        time.sleep(3)
        # 成功登陆后能找到该元素：
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#app > div > div.main-container > div > div.navbar > div.right-menu > div.avatar-container.el-dropdown > div > img")

        self.driver.save_screenshot('../images/login/login8.png')
        self.driver.quit()

    # 失败案例: 错误的账号, 密码:
    def LoginFailTest1(self):
        # 错误的账号:
        Login.Init(self, "aaa", "admin")
        self.autoIdentifyCode()
        self.driver.find_element(By.CSS_SELECTOR, "#app > div > form > button").click()
        time.sleep(0.5)
        self.driver.save_screenshot('../images/login/login1.png')

        # 错误的密码:
        Login.Init(self, "admin", "aaaaa")
        self.autoIdentifyCode()
        self.driver.find_element(By.CSS_SELECTOR, "#app > div > form > button").click()
        time.sleep(0.5)
        self.driver.save_screenshot('../images/login/login2.png')

    # 失败案例: 用户名为空/密码为空:
    def LoginFailTest2(self):
        # 用户名为空:
        Login.Init(self, "", "admin")
        self.autoIdentifyCode()
        self.driver.find_element(By.CSS_SELECTOR, "#app > div > form > button").click()
        self.driver.save_screenshot('../images/login/login3.png')

        # 密码为空:
        Login.Init(self, "admin", "")
        self.autoIdentifyCode()
        self.driver.find_element(By.CSS_SELECTOR, "#app > div > form > button").click()
        self.driver.save_screenshot('../images/login/login4.png')

    # 验证码为空:
    def LoginFailTest3(self):
        Login.Init(self, "admin", "admin")
        self.driver.find_element(By.CSS_SELECTOR, "#app > div > form > div:nth-child(4) > div > div > input").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#app > div > form > button").click()
        time.sleep(0.5)
        self.driver.save_screenshot('../images/login/login5.png')

    # 错误的验证码:
    def LoginFailTest4(self):
        Login.Init(self, "admin", "admin")
        self.driver.find_element(By.CSS_SELECTOR, "#app > div > form > div:nth-child(4) > div > div > input").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#app > div > form > div:nth-child(4) > div > div > input") \
            .send_keys('1111')

        self.driver.find_element(By.CSS_SELECTOR, "#app > div > form > button").click()
        time.sleep(0.5)
        self.driver.save_screenshot('../images/login/login6.png')

    # 不选择用户类型:
    def LoginFailTest5(self):
        self.driver.refresh()
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#app > div > form > div:nth-child(2) > div > div.el-input > input").send_keys(
            'admin')  # 账号
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#app > div > form > div:nth-child(3) > div > div.el-input > input").send_keys(
            'admin')  # 密码
        self.autoIdentifyCode()
        self.driver.find_element(By.CSS_SELECTOR, "#app > div > form > button").click()
        time.sleep(0.5)
        self.driver.save_screenshot('../images/login/login7.png')


login = Login()

# # 错误的账号: aaa, 密码: admin  / 账号: admin, 密码: aaa
#login.LoginFailTest1()

# 账号为空/密码为空:
#login.LoginFailTest2()

# 验证码为空:
#login.LoginFailTest3()

# 错误的验证码:
#login.LoginFailTest4()

# 不选择用户类型:
#login.LoginFailTest5()

# 正确的账号: admin 密码: admin
#login.LoginSucTest()
